<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Runforever extends  Controller
{
    public function _initialize()
    {
        header('Content-Type:text/html; charset=utf-8');
        //判断用户是否登录
        //判断权限
        //登录有效期
    }

    //ajax用于一直运行{:url('index/dataapi/create_trading_sum_today')}
    public function run_forever(){
        return $this->fetch('run_forever');
    }

    //用于v3平台代理商和商户看板
    public function now_province(){
        $agent_id = Request::instance()->param('agent_id');
        $merchant_id = Request::instance()->param('merchant_id');
        $user_type = Request::instance()->param('user_type');
        $np = Db::name('now_provinces');
        if ($user_type=='10'){
            $res = $np->where("agent_id='$agent_id' and user_type='10'")->select();
        }else{
            $res = $np->where("merchant_id='$merchant_id' and user_type='20'")->select();
        }
        if (!empty($res)){
            // echo $res[0]['province_name'];
            echo $_GET['callback'].'('.json_encode($res[0]['province_name']).')';
        }else{
            $data['agent_id'] = $agent_id;
            $data['merchant_id'] = $merchant_id;
            $data['user_type'] = $user_type;
            $data['province_name'] = 'china';
            $np->insert($data);
            // echo '0';
            echo $_GET['callback'].'('.json_encode(0).')';

        }
    }

    public function change_province(){
        $agent_id = Request::instance()->param('agent_id');
        $merchant_id = Request::instance()->param('merchant_id');
        $user_type = Request::instance()->param('user_type');
        $data['province_name'] = Request::instance()->param('province_name');
        $np = Db::name('now_provinces');
        if ($user_type=='10'){
            $res = $np->where("agent_id='$agent_id' and user_type='10'")->update($data);
        }else{
            $res = $np->where("merchant_id='$merchant_id' and user_type='20'")->update($data);
        }
        echo $res;
//        var_dump($res);
//        if (!empty($res)){
//            echo $res[0]['province_name'];
//        }else{
//            echo '0';
//        }
    }
}
